package cadc.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.windpicker.common.utils.DateUtil;
import cadc.models.Account;

public class AccountDao extends JdbcDaoSupport {
	
	public Account findAccountByName(String name){
		String sql = "select * from accounts where name=?";
		List<Account> items = super.getJdbcTemplate().query(sql, new AccountRowMapper(), name);
		if(items.isEmpty()||items.size()==0)return null;
		return items.get(0);
	}
	
	public List<Account> listAccountLike(String pre){
		String sql = "select * from accounts where name like '"+pre+"%'";
		return super.getJdbcTemplate().query(sql, new AccountRowMapper());
	}
	
	class AccountRowMapper implements RowMapper<Account>{
		@Override
		public Account mapRow(ResultSet rs, int arg1) throws SQLException {
			Account account = new Account();
			account.setId(rs.getLong("id"));
			account.setName(rs.getString("name"));
			account.setPassword(rs.getString("password"));
			account.setRegistrationTime(DateUtil.stringToDate(rs.getString("registrationTime")));
			return account;
		}	
	}
	
}
